{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy科学计算  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy 是 python科学计算的核心库。PYTHON里涉及到科学计算的包括Pandas,sklearn等都是基于numpy进行二次开发包装的。numpy功能非常强大，其余scipy构建了强大的PYTHON数理计算功能，函数接口丰富复杂。\n",
    "\n",
    "<img src=\"http://wx1.sinaimg.cn/mw690/d409b13egy1fo90o5jtpqj211i09wjuc.jpg\" width = \"500\" height = \"300\" alt=\"图片名称\" align=center />\n",
    "\n",
    "对于本次课程来说，我们重点学习的是以下几点：\n",
    "    1. 数组的定义和应用\n",
    "    2. 数组元素的索引选取\n",
    "    3. 数组的计算\n",
    "    4. 线性代数的运行计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Arrays\n",
    "\n",
    "array用来存储同类型的序列数据，能够被非负整数进行索引。 维度的数量就是array的秩(rank)。\n",
    "\n",
    "我们可以通过python的列表来创建array,并且通过方括号进行索引获取元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  3  4  6 10]\n",
      "5\n",
      "(5,)\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "a = np.array([1,3,4,6,10])\n",
    "\n",
    "print(a)\n",
    "\n",
    "print(a.size)\n",
    "print(a.shape)\n",
    "print(a[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 4)\n"
     ]
    }
   ],
   "source": [
    "# 二维数组\n",
    "\n",
    "b = np.array([[1,2,3,4],[5,6,7,8]])\n",
    "print(b.shape)\n",
    "#b[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 4)\n"
     ]
    }
   ],
   "source": [
    "# 三维数组\n",
    "b = np.array([[[1,2,3,4],[5,6,7,8]]])\n",
    "print(b.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建Array\n",
    "numpy提供了内置的函数来创建一些特殊的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "b = np.ones([3,3])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros_like(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  0.,  0.],\n",
       "       [ 0.,  1.,  0.],\n",
       "       [ 0.,  0.,  1.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Array的常用属性和方法\n",
    "\n",
    "* 统计计算\n",
    "* 排序\n",
    "* 按照大小查索引\n",
    "* 条件查找\n",
    "* shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.rand(3,4)\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.22115568, 0.85374181, 0.0971923 , 0.7749461 ],\n",
       "       [0.94083223, 0.02671383, 0.73202248, 0.18316195],\n",
       "       [0.97417192, 0.9745558 , 0.03574694, 0.00215869]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.73450402, 1.04862103, 2.41451299])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum(axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.197638046584345\n",
      "[1.73450402 1.04862103 2.41451299]\n",
      "[1.99897112 0.50502153 1.77966338 0.91398202]\n"
     ]
    }
   ],
   "source": [
    "print(np.sum(a))\n",
    "print(np.sum(a,axis = 1))\n",
    "print(np.sum(a,axis = 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.27852909235886786"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(a)\n",
    "np.std(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.56301444, 0.23586479, 0.92482021, 0.01080459],\n",
       "       [0.70409035, 0.0027975 , 0.28153161, 0.06020156],\n",
       "       [0.73186633, 0.26635924, 0.57331156, 0.84297587]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.22115568, 0.02671383, 0.03574694, 0.00215869],\n",
       "       [0.94083223, 0.85374181, 0.0971923 , 0.18316195],\n",
       "       [0.97417192, 0.9745558 , 0.73202248, 0.7749461 ]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 排序\n",
    "np.sort(a,axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 2, 0],\n",
       "       [1, 0, 3, 2],\n",
       "       [0, 2, 1, 3]], dtype=int64)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Returns the indices that would sort this array.\n",
    "# 返回的是数组值从小到大的索引值\n",
    "a.argsort(axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 1, 3],\n",
       "       [1, 0, 3, 2],\n",
       "       [3, 1, 2, 0]], dtype=int64)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.flipud(a.argsort(axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.22115568, 0.85374181, 0.0971923 , 0.7749461 ],\n",
       "       [0.94083223, 0.02671383, 0.73202248, 0.18316195],\n",
       "       [0.97417192, 0.9745558 , 0.03574694, 0.00215869]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 1, 0], dtype=int64)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Returns the indices of the maximum values along an axis.\n",
    "np.argmax(a,axis =0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.52912576,  0.98066039,  0.89180773])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(a,axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.56301444, 0.23586479, 0.92482021, 0.01080459],\n",
       "       [0.70409035, 0.0027975 , 0.28153161, 0.06020156],\n",
       "       [0.73186633, 0.26635924, 0.57331156, 0.84297587]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 1, 2, 2, 2], dtype=int64),\n",
       " array([0, 2, 0, 0, 2, 3], dtype=int64))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Return elements, either from `x` or `y`, depending on `condition`.\n",
    "# If only `condition` is given, return ``condition.nonzero()``\n",
    "np.where(a>0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([1,2,3,4,5,6,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([4, 5, 6], dtype=int64),)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(b >4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[np.where(b >4)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Shape改变\n",
    "一个数组的 shape 是由轴及其元素数量决定的，它一般由一个整型元组表示，且元组中的整数表示对应维度的元素数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 6)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.random.randint(1,100,size =(5,6))\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 68, 87, 50, 28, 70],\n",
       "       [15, 96, 15, 28,  2, 15],\n",
       "       [53, 90, 21, 99,  3, 90],\n",
       "       [61, 53, 75, 52, 42, 66],\n",
       "       [34, 92, 11, 49, 26,  3]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个数组的 shape 可以由许多方法改变。例如以下三种方法都可输出一个改变 shape 后的新数组，它们都不会改变原数组。其中 reshape 方法在实践中会经常用到，因为我们需要改变数组的维度以执行不同的运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 68, 87, 50, 28, 70, 15, 96, 15, 28,  2, 15, 53, 90, 21, 99,  3,\n",
       "       90, 61, 53, 75, 52, 42, 66, 34, 92, 11, 49, 26,  3])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 68, 87],\n",
       "       [50, 28, 70],\n",
       "       [15, 96, 15],\n",
       "       [28,  2, 15],\n",
       "       [53, 90, 21],\n",
       "       [99,  3, 90],\n",
       "       [61, 53, 75],\n",
       "       [52, 42, 66],\n",
       "       [34, 92, 11],\n",
       "       [49, 26,  3]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(10,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[16, 68, 52, 99,  2],\n",
       "       [24, 14, 19, 44, 33],\n",
       "       [15, 96, 52, 93, 68],\n",
       "       [98,  4, 92, 19, 63],\n",
       "       [11, 84,  8, 47,  8],\n",
       "       [46, 23, 23, 43, 84]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ravel() 和 flatten() 都是将多维数组降位一维，flatten() 返回一份新的数组，且对它所做的修改不会影响原始数组."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([16, 24, 15, 98, 11, 46, 68, 14, 96,  4, 84, 23, 52, 19, 52, 92,  8,\n",
       "       23, 99, 44, 93, 19, 47, 43,  2, 33, 68, 63,  8, 84])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果在 shape 变换中一个维度设为 - 1，那么这一个维度包含的元素数将会被自动计算。如下所示，a 一共有 30 个元素，在确定一共有 3 行后，-1 会自动计算出应该需要 10 列才能安排所有的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 68, 87],\n",
       "       [50, 28, 70],\n",
       "       [15, 96, 15],\n",
       "       [28,  2, 15],\n",
       "       [53, 90, 21],\n",
       "       [99,  3, 90],\n",
       "       [61, 53, 75],\n",
       "       [52, 42, 66],\n",
       "       [34, 92, 11],\n",
       "       [49, 26,  3]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(10,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 68, 87, 50, 28, 70, 15, 96, 15, 28,  2, 15, 53, 90, 21, 99,  3,\n",
       "       90, 61, 53, 75, 52, 42, 66, 34, 92, 11, 49, 26,  3])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 68, 87, 50, 28, 70, 15, 96, 15, 28],\n",
       "       [ 2, 15, 53, 90, 21, 99,  3, 90, 61, 53],\n",
       "       [75, 52, 42, 66, 34, 92, 11, 49, 26,  3]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 随机数\n",
    "\n",
    "numpy可以根据一定的规则创建随机数，随机数的使用会在后面概率论，数据挖掘的时候经常用到。\n",
    "\n",
    "官方主页[RANDOM](https://docs.scipy.org/doc/numpy/reference/routines.random.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "常用的一些方法：\n",
    "\n",
    "* rand(d0, d1, ..., dn)\tRandom values in a given shape.\n",
    "* randn(d0, d1, ..., dn)\tReturn a sample (or samples) from the “standard normal” distribution.\n",
    "* randint(low[, high, size, dtype])\tReturn random integers from low (inclusive) to high (exclusive).\n",
    "* random([size])\tReturn random floats in the half-open interval [0.0, 1.0).\n",
    "* sample([size])\tReturn random floats in the half-open interval [0.0, 1.0).\n",
    "* choice(a[, size, replace, p])\tGenerates a random sample from a given 1-D array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.30370379, 0.54753942, 0.35059235, 0.84145832, 0.5956049 ,\n",
       "       0.16281789, 0.25731931, 0.45214264, 0.84460273, 0.81216332])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(10)\n",
    "#np.random.rand(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.51409848e-01,  4.22360041e-01,  1.71040523e+00, -7.04540001e-01,\n",
       "        1.13518031e+00, -1.46299472e+00, -2.56903806e+00, -2.65053469e-01,\n",
       "        2.39246210e+00,  1.25045402e+00,  2.58955558e-02, -5.77686234e-01,\n",
       "        6.18851283e-01, -1.14703856e+00,  7.29162925e-02, -6.01940000e-01,\n",
       "       -1.24232346e+00, -1.74068012e-01,  6.53268860e-01, -6.70071969e-02,\n",
       "       -4.13291707e-01, -3.57338719e-01, -1.87471196e+00,  2.28190754e-01,\n",
       "       -5.31114876e-01,  6.76067986e-01,  2.36430150e+00, -1.01725304e+00,\n",
       "       -7.91914758e-01,  1.61012062e+00,  6.11852173e-01, -2.98429820e-01,\n",
       "       -5.63288570e-01,  1.26884981e+00,  3.52867719e-01, -6.14166366e-01,\n",
       "        1.95929832e-01, -2.70721292e-01,  5.47679986e-01,  8.31818396e-02,\n",
       "       -5.44712773e-01, -3.47575131e-01, -1.96149073e-01, -1.02551975e+00,\n",
       "        2.73810997e-01, -1.35373086e+00, -1.67351920e+00,  9.67888609e-01,\n",
       "       -2.34989043e+00, -2.45327148e-01,  5.72575933e-01, -8.49816923e-01,\n",
       "       -4.02756936e-01,  8.33148761e-04,  4.94647527e-01,  9.50147492e-01,\n",
       "        9.68079373e-01,  2.17783879e-01,  1.12443744e+00, -1.54620562e-01,\n",
       "        1.43850948e+00, -9.06713944e-01,  4.58393527e-01,  5.71542550e-02,\n",
       "       -4.21954132e-01,  9.52296604e-01,  8.38290898e-01,  6.16173779e-01,\n",
       "        1.69331264e+00,  1.12989476e+00, -9.17813406e-01,  1.32494256e+00,\n",
       "        3.45115825e-01, -5.98580467e-01, -5.70738749e-01, -4.50669336e-01,\n",
       "        3.80910499e-01,  1.93076147e-01,  2.13657489e-01, -9.53152147e-01,\n",
       "       -2.29203917e-01, -2.47963923e+00, -1.71694121e+00,  1.91125414e+00,\n",
       "       -2.77007639e-01, -7.89771971e-01,  2.11998021e-01,  5.42237806e-01,\n",
       "        8.11283668e-01,  1.87124154e-01, -9.02544965e-01, -1.42373476e-01,\n",
       "        1.35358658e+00, -2.04209530e-01, -1.62619559e+00,  7.14605750e-01,\n",
       "        4.42036855e-01, -6.54754592e-01,  1.09082164e+00,  5.52124450e-02])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# randn 具有标准正态分布\n",
    "np.random.randn(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 3, 1, 4],\n",
       "       [4, 3, 5, 6],\n",
       "       [4, 3, 9, 2]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# randint 左闭右开\n",
    "np.random.randint(10)\n",
    "np.random.randint(1,10,size = (3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\KZCF\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: DeprecationWarning: This function is deprecated. Please call randint(1, 5 + 1) instead\n",
      "  \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [1, 4],\n",
       "       [5, 5]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#random_integers 左右均闭\n",
    "np.random.random_integers(1,5, size=(3,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.83695398, 0.52965557],\n",
       "       [0.11698992, 0.45478618]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random((2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 7, 2, 6],\n",
       "       [1, 5, 3, 1],\n",
       "       [8, 1, 8, 7]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.choice(10,(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5.  ,  7.08,  7.08,  4.  ],\n",
       "       [ 4.  ,  4.  ,  7.08,  4.  ],\n",
       "       [ 7.08,  7.08,  7.08,  7.08]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.choice([1,4,5,7.08],(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.75275929,  2.70428584,  1.39196365,  0.59195091, -2.06388816,\n",
       "       -2.06403288, -2.65149833,  2.19705687,  0.60669007,  1.24843547])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机种子固定随机数\n",
    "rnd = np.random.RandomState(42)\n",
    "rnd.uniform(-3, 3, size=10)  #随机从[-3，3)间产生100个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.24713211, -0.80182894, -0.26358009,  1.71105577],\n",
       "       [-1.80195731,  0.08540663,  0.55448741, -2.72129752],\n",
       "       [ 0.64526911, -1.97685526, -2.60969044,  2.69331322]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rnd.uniform(-3, 3, size=(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 7, 6, 8],\n",
       "       [7, 4, 1, 4],\n",
       "       [7, 9, 8, 8]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=rnd.randint(10,size = (3,4))\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 1],\n",
       "       [1, 0, 0, 0],\n",
       "       [2, 2, 2, 2]], dtype=int64)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.argsort(axis = 0)  #按列排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 1, 3],\n",
       "       [2, 1, 3, 0],\n",
       "       [0, 2, 3, 1]], dtype=int64)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.argsort(axis = 1)  #按行排序(默认)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 3, 1],\n",
       "       [2, 1, 3, 0],\n",
       "       [0, 2, 1, 3]], dtype=int64)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.flipud(x.argsort(axis = 1))  #将矩阵上下翻转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 2, 0],\n",
       "       [0, 3, 1, 2],\n",
       "       [1, 3, 2, 0]], dtype=int64)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.fliplr(x.argsort(axis = 1))  #将数组左右翻转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X1.ravel() <class 'numpy.ndarray'> (100,)\n",
      "X2.ravel() <class 'numpy.ndarray'> (100,)\n",
      "X_grid <class 'numpy.ndarray'> (100, 2)\n"
     ]
    }
   ],
   "source": [
    "#从坐标向量返回坐标矩阵\n",
    "from sklearn.datasets import make_blobs\n",
    "X, y = make_blobs(centers=2, random_state=4, n_samples=30)\n",
    "x_min, x_max = 0,5\n",
    "y_min, y_max = 5,10\n",
    "xx = np.linspace(x_min, x_max, 10)\n",
    "yy = np.linspace(y_min, y_max, 10)\n",
    "X1, X2 = np.meshgrid(xx, yy)  #从坐标向量返回坐标矩阵，它讲 xx 变成了矩阵 X1 的行向量，yy 变成了矩阵 X2 的列向量。\n",
    "print('X1.ravel()',type(X1.ravel()),X1.ravel().shape)  #ravel()  将多维数组降为一维\n",
    "print('X2.ravel()',type(X2.ravel()),X2.ravel().shape)\n",
    "X_grid = np.c_[X1.ravel(), X2.ravel()]  #np.c_  将两个数组左右平行合并\n",
    "print('X_grid',type(X_grid),X_grid.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "?np.random.choice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组的索引\n",
    "\n",
    "**切片**选取类似于list，但是array可以是多维度的，因此我们需要指定每一个维度上的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) # 2维数组，shape = 3 * 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5],\n",
       "       [9]])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:3,0:1]\n",
    "#a[:,:1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**整数索引**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5, 10])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[[1,2],[0,1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**布尔型索引**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5,  6,  7,  8,  9, 10, 11, 12])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a >4\n",
    "a[a>4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**图解索引**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<img src=\"http://wx1.sinaimg.cn/mw690/d409b13egy1fo90ob733bj218m0hyjz3.jpg\" width = \"600\" height = \"400\" alt=\"图片名称\" align=left />\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://wx2.sinaimg.cn/mw690/d409b13egy1fo90ohhyecj21020kaage.jpg\" width = \"600\" height = \"400\" alt=\"图片名称\" align=left />\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组数学"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.random.random([3,4])\n",
    "b = np.random.random([3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.76004347,  0.4258463 ,  0.08326275,  0.93285095],\n",
       "       [ 0.87100438,  0.89512213,  0.66405053,  0.37225536],\n",
       "       [ 0.59545034,  0.41663924,  0.51195997,  0.77346328]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.48043437,  2.3062315 ,  2.37038885,  2.24346901],\n",
       "       [ 2.57617282,  2.45257504,  2.59148344,  2.9932576 ],\n",
       "       [ 2.01946187,  2.9662433 ,  2.59164076,  2.89874224]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7.60043471,  4.25846302,  0.83262751,  9.32850955],\n",
       "       [ 8.71004379,  8.95122127,  6.64050534,  3.72255362],\n",
       "       [ 5.95450342,  4.16639238,  5.11959969,  7.73463281]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.56674767,  0.83059901,  0.08406071,  0.60134785],\n",
       "       [ 0.68305575,  0.85945331,  0.50625002,  0.65044408],\n",
       "       [ 0.00539243,  0.39640508,  0.43254736,  0.94011285]])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   1.34106147,    0.51269782,    0.99050729,    1.5512668 ],\n",
       "       [   1.27515856,    1.04150175,    1.31170472,    0.57230955],\n",
       "       [ 110.42337422,    1.05104414,    1.18359286,    0.82273451]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Elementwise\n",
    "a  + b\n",
    "a - b\n",
    "a * b\n",
    "a / b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.720169  ,  0.67768136,  1.59930077,  0.40340647],\n",
       "       [ 0.78913739,  0.55002961,  0.7516446 ,  0.42565485],\n",
       "       [ 1.71390674,  0.76294821,  3.74288752,  0.50604784]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Elementwise\n",
    "np.add(a,b)\n",
    "np.subtract(a,b)\n",
    "np.multiply(a,b)\n",
    "np.divide(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*是元素力度的计算(Elementwise),并不是矩阵计算。我们使用dot函数进行内积求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.35242743,  1.53406623,  1.08590637],\n",
       "       [ 1.51680278,  1.94256711,  0.99672314],\n",
       "       [ 1.19168644,  1.52708211,  1.11695854]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# shape(a) = 3*4  shape(b.T) = 4*3\n",
    "a.dot(b.T) # (3*4) * (4*3) = 3 * 3\n",
    "np.dot(a,b.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.5 7.0\n",
      "0.0 9.0\n",
      "(750,) (900, 750)\n",
      "(900,) (900, 750)\n",
      "[[-0.5  -0.49 -0.48 ...  6.97  6.98  6.99]\n",
      " [-0.5  -0.49 -0.48 ...  6.97  6.98  6.99]\n",
      " [-0.5  -0.49 -0.48 ...  6.97  6.98  6.99]\n",
      " ...\n",
      " [-0.5  -0.49 -0.48 ...  6.97  6.98  6.99]\n",
      " [-0.5  -0.49 -0.48 ...  6.97  6.98  6.99]\n",
      " [-0.5  -0.49 -0.48 ...  6.97  6.98  6.99]]\n",
      "[[0.   0.   0.   ... 0.   0.   0.  ]\n",
      " [0.01 0.01 0.01 ... 0.01 0.01 0.01]\n",
      " [0.02 0.02 0.02 ... 0.02 0.02 0.02]\n",
      " ...\n",
      " [8.97 8.97 8.97 ... 8.97 8.97 8.97]\n",
      " [8.98 8.98 8.98 ... 8.98 8.98 8.98]\n",
      " [8.99 8.99 8.99 ... 8.99 8.99 8.99]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "X = np.array([[4, 7], [3.5, 8], [3.1, 6.2], [0.5, 1], [1, 2], [1.2, 1.9], [6, 2], [5.7, 1.5], [5.4, 2.2]])\n",
    "y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])\n",
    "\n",
    "x_min, x_max = min(X[:, 0]) - 1.0, max(X[:, 0]) + 1.0\n",
    "y_min, y_max = min(X[:, 1]) - 1.0, max(X[:, 1]) + 1.0\n",
    "print(x_min, x_max)\n",
    "print(y_min, y_max)\n",
    "\n",
    "step_size = 0.01\n",
    "x_values, y_values = np.meshgrid(np.arange(x_min, x_max, step_size), np.arange(y_min, y_max, step_size))\n",
    "#print(np.meshgrid(np.arange(x_min, x_max, step_size), np.arange(y_min, y_max, step_size))[0].shape)\n",
    "print(np.arange(x_min, x_max, step_size).shape,x_values.shape)\n",
    "print(np.arange(y_min, y_max, step_size).shape,y_values.shape)\n",
    "print(x_values)\n",
    "print(y_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5  6]\n",
      " [11 22 33 44 55 66]]\n",
      "[[ 1  2  3]\n",
      " [11 22 33]\n",
      " [ 4  5  6]\n",
      " [44 55 66]]\n",
      "[ 1  2  3  4  5  6 11 22 33 44 55 66]\n",
      "[ 1  2  3 11 22 33  4  5  6 44 55 66]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1,2,3],[11,22,33]])\n",
    "b = np.array([[4,5,6],[44,55,66]])\n",
    "c = np.c_[a,b]  #左右合并\n",
    "r = np.r_[a,b]  #上下合并\n",
    "print(c)\n",
    "print(r)\n",
    "\n",
    "print(c.ravel())  #展开\n",
    "print(r.ravel())  #展开"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性代数\n",
    "numpy和scipy可以进行线性代数的计算，但是我们目前还没补充线性代数知识。因此这一章节我们会挪动到 线性代数 理论知识章节进行讲解！"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": false,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {
    "height": "322px",
    "left": "1145px",
    "right": "20px",
    "top": "238px",
    "width": "212px"
   },
   "toc_section_display": "none",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
